﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using VkontakteCore;
using VkontakteInfrastructure.Model;
using VkontakteInfrastructure;
namespace VkVideoPlayer.UI.Services
{
	public class ErrorHandlerService : IErrorHandlerService
	{
		 private readonly IVkontakteApi vkontakteApi;
        private readonly Application application;
        private readonly INavigationService navigationService;

		public ErrorHandlerService(IVkontakteApi vkontakteApi, INavigationService navigationService)
        {
            this.vkontakteApi = vkontakteApi;
            this.application = application;
            this.navigationService = navigationService;
        }

        public bool HandleError(Error error)
        {
			if (getError(error.ErrorCode) == VKErrors.NotAuth)
            {
                vkontakteApi.DeleteContext();
                navigationService.NavigatToAuthorizationPage();
                return true;
            }
            return false;
        }

		private VKErrors getError(string src)
		{
			int errcode = -1;
			if(int.TryParse(src,out errcode))
			{
				try
				{
					return (VKErrors) errcode;
				}
				catch (Exception)
				{
					return VKErrors.NoError;
				}
			}
			return VKErrors.NoError; 
		}
	}
}
